home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / x / animutil / xanim229.lha / xanim / xanim_txt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-08  |  4.5 KB  |  180 lines

  1.  
  2. /*
  3.  * xanim_txt.c
  4.  *
  5.  * Copyright (C) 1990,1991,1992 by Mark Podlipec. 
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed
  9.  * without fee provided that this copyright notice is preserved 
  10.  * intact on all copies and modified copies.
  11.  * 
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include "mytypes.h"
  22. #include "xanim.h"
  23. #include "xanim_gif.h"
  24.  
  25. int Is_TXT_File();
  26. void TXT_Read_File();
  27.  
  28. /*
  29.  * This file will open the filename passed to it and determine if
  30.  * it is a txt91 file. If it is it returns TRUE, else FALSE. It
  31.  * closes the file before returning.
  32.  */
  33. int Is_TXT_File(filename)
  34. char *filename;
  35. {
  36.  FILE *fp;
  37.  ULONG firstword;
  38.  
  39.  if ( (fp=fopen(filename,"r")) == 0)
  40.  { fprintf(stderr,"can't open %s\n",filename); TheEnd();}
  41.  /* by reading bytes we can ignore big/little endian problems */
  42.  firstword  = (fgetc(fp) & 0xff) << 24;
  43.  firstword |= (fgetc(fp) & 0xff) << 16;
  44.  firstword |= (fgetc(fp) & 0xff) <<  8;
  45.  firstword |= (fgetc(fp) & 0xff);
  46.  fclose(fp);
  47.  
  48. /*                   t x t 9     so we ignore the 1 */
  49.  if (firstword == 0x74787439) return(TRUE);
  50.  return(FALSE);
  51. }
  52.  
  53. static char gif_file_name[256];
  54.  
  55. /*
  56.  * This file parse the txt animation file and converts it into actions. 
  57.  *
  58.  */
  59. void TXT_Read_File(fname)
  60. char *fname;
  61. {
  62.  FILE *fp;
  63.  int ret,i;
  64.  int num_of_files,txtframe_num;
  65.  int *txt_act_lst;
  66.  
  67.  if ( (fp=fopen(fname,"r"))==0)
  68.  { 
  69.   fprintf(stderr,"Can't open %s for reading.\n",fname); 
  70.   TheEnd();
  71.  }
  72.  
  73.  /* read and throw away txt91 header 
  74.   */
  75.  fscanf(fp,"%*s",gif_file_name);
  76.  
  77.  /* Read the number of files
  78.   */
  79.  fscanf(fp,"%ld",&num_of_files);
  80.  if (num_of_files<=0)
  81.  {
  82.   fprintf(stderr,"num_of_file is wierd=%ld\n",num_of_files);
  83.   fclose(fp);
  84.   TheEnd();
  85.  }
  86.  
  87.  txt_act_lst = (int *) malloc( sizeof(int) * (num_of_files + 1));
  88.  /* Read in the GIF files, use only the 1st one's colormap
  89.   */
  90.  for(i=0;i<num_of_files;i++)
  91.  {
  92.   fscanf(fp,"%s",gif_file_name);
  93.   fprintf(stderr,"Reading %s\n",gif_file_name);
  94.   txt_act_lst[i] = action_cnt;
  95.   if (i==0) GIF_Read_File(gif_file_name,1);
  96.   else      GIF_Read_File(gif_file_name,0);
  97.  }
  98.  txt_act_lst[num_of_files] = action_cnt;
  99.  
  100.  /* Check for Frame list at end of images.
  101.   */
  102.  ret=fscanf(fp,"%ld",&txtframe_num);
  103.  if ( (ret==1) && (txtframe_num>=0))
  104.  {
  105.   int *txt_frames,tmp_txtframe,num_valid_txtframes;
  106.   int numof_frames,j,k;
  107.  
  108.   /* read in txt frame list, keep track of actual frames since each txt_frame
  109.    * can have several frames(cmaps and images);
  110.    */
  111.   txt_frames = (int *) malloc(txtframe_num * sizeof(int) );
  112.   numof_frames = 0;
  113.   num_valid_txtframes = 0;
  114.   for(i=0; i<txtframe_num; i++)
  115.   {
  116.    ret = fscanf(fp,"%ld",&tmp_txtframe);
  117.    if ( (ret==1) && (tmp_txtframe >= 0) && (tmp_txtframe < num_of_files) )
  118.    {
  119.     txt_frames[num_valid_txtframes] = tmp_txtframe;
  120.     numof_frames += txt_act_lst[ tmp_txtframe + 1 ] 
  121.                                - txt_act_lst[ tmp_txtframe ];
  122.     num_valid_txtframes++;
  123.    }
  124.    else
  125.    {
  126.     fprintf(stderr,"TXT_READ: bad frame number (%ld) in frame list\n",
  127.                             tmp_txtframe);
  128.    }
  129.   }
  130.   /* Allocate a frame_lst of that size.
  131.    */
  132.   anim[anim_cnt].frame_lst = (int *)malloc(sizeof(int) * (numof_frames + 1));
  133.   if (anim[anim_cnt].frame_lst == NULL)
  134.                    TheEnd1("TXT_ANIM: couldn't malloc for frame_lst\0");
  135.  
  136.   j = 0;
  137.   for(i=0; i<num_valid_txtframes; i++)
  138.   {
  139.    for(k=txt_act_lst[ txt_frames[i] ]; k < txt_act_lst[ txt_frames[i]+1 ]; k++)
  140.    {
  141.     anim[anim_cnt].frame_lst[j] = k;
  142.     j++;
  143.    }
  144.   }
  145.  
  146.   free(txt_frames);
  147.   /* put into animfile header
  148.    */
  149.   anim[anim_cnt].frame_lst[numof_frames] = -1;
  150.   anim[anim_cnt].loop_frame = 0;
  151.   anim[anim_cnt].last_frame = numof_frames - 1;
  152.  }
  153.  /* If there is no frame list then create a sequential one
  154.   */
  155.  else
  156.  {
  157.   int numof_actions;
  158.  
  159.   /* the number of actions is the number of frames 
  160.    */
  161.   numof_actions = action_cnt - action_start;
  162.   /* malloc frame array plus 1 for ending frame
  163.    */
  164.   anim[anim_cnt].frame_lst = (int *)malloc(sizeof(int) * (numof_actions+1));
  165.   if (anim[anim_cnt].frame_lst == NULL)
  166.                    TheEnd1("TXT_ANIM: couldn't malloc for frame_lst\0");
  167.   for(i=0; i < numof_actions; i++) anim[anim_cnt].frame_lst[i]=action_start+i;
  168.  
  169.   /* put into animfile header
  170.    */
  171.   anim[anim_cnt].frame_lst[numof_actions] = -1;
  172.   anim[anim_cnt].loop_frame = 0;
  173.   anim[anim_cnt].last_frame = numof_actions - 1;
  174.  }
  175.  
  176.  free(txt_act_lst);
  177.  fclose(fp);
  178. }
  179.  
  180.